import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import {ReplaySubject} from 'rxjs';
import {CBoardEChartRender} from './utils/CBoardEChartRender';

declare var $: any;
import * as echarts from 'echarts';
import Echarts = echarts.Echarts;
import * as numbro from 'numbro';
import * as _ from 'underscore';
import {CBoardTableRender} from './utils/CBoardTableRender';

@Injectable()
export class ChartTableService {
    constructor(
      private router: Router
    ) {}
    public render(containerDom, option, scope, persist, drill){
      if (option == null) {
        containerDom.html("<div class=\"alert alert-danger\" role=\"alert\">No Data!</div>");
        return;
      }
      var height;
      scope ? height = scope.myheight - 20 : null;
      return new CBoardTableRender(containerDom, option, drill).do(height, persist);
    }

    public parseOption(data){
      console.log(data);
      var tableOption = this.chartDataProcess(data.chartConfig, data.keys, data.series, data.data, data.seriesConfig);
      console.log(tableOption);
      return tableOption;
    }
  /**
   * casted_keys:行维数据
   * casted_values:列维数据
   * aggregate_data:表格数据
   * newValuesConfig:legend
   */
    private chartDataProcess(chartConfig,casted_keys, casted_values, aggregate_data,newValuesConfig){
    /** table数据列 */
    var keysList = casted_keys,
      keyArr = [],
      emptyList = [],
      /** table标题列 */
      keyLength = chartConfig.keys.length,
      rowHeaderLength = keysList[0] ? keysList[0].length : 0;
    // Array.matrix = function (numrows, numcols, initial) {
        // var arr = [];
        // for (var a = 0; a < numrows; ++a) {
        //   var columns = [];
        //   for (var s = 0; s < numcols; ++s) {
        //     columns[s] = initial;
        //   }
        //   arr[a] = columns;
        // }
        // return arr;
    // };
    var table_data = this.matrix(keysList.length, rowHeaderLength, 0);
    /** 获取table的列维数据 */
    for (var h = 0; h < rowHeaderLength; h++) {
      for (var k = 0; k < keysList.length; k++) {
        table_data[k][h] = {
          property: 'column_key',
          data: keysList[k][h]
        };
      }
    }
    /*** 获取table的body数据 */
    for (var i = 0; i < casted_values.length; i++) {
      var joined_values = casted_values[i].join('-');
      var formatter = newValuesConfig[joined_values].formatter;
      for (var j = 0; j < casted_keys.length; j++) {
        if (!_.isUndefined(aggregate_data[i][j])) {
          var raw =Number( aggregate_data[i][j]);
          var style=function () {
            var flag=false;
            if(chartConfig.criticalValue){

              switch (chartConfig.judge){

                case 'string:>':
                  if( raw>chartConfig.criticalValue){
                    flag=true;
                  }
                  break;
                case 'string:<':
                  if (raw<chartConfig.criticalValue){
                    flag=true;
                  }
                  break;
                case 'string:>=':
                  if( raw>=chartConfig.criticalValue){
                    flag=true;
                  }
                  break;
                case 'string:<=':
                  if (raw<=chartConfig.criticalValue){
                    flag=true;
                  }
                  break;
                default:flag=false;
                  break;
              }
            }



            if(flag){
              if(chartConfig.tableStyle){
                return chartConfig.tableStyle;
              }else {
                return '';
              }
            }else {
              return '';
            }
          }
          table_data[j][i + keyLength] = {
            property: 'data',
            data: formatter ? numbro(raw).format(formatter) : raw,
            raw: raw,
            style:style()
          };
        } else {
          table_data[j][i + keyLength] = {
            property: 'data',
            data: ''
          };
        }
      }
    }
    /** 获取列维数据 */
    var column_header = this.matrix(chartConfig.groups.length + 1, casted_values.length, 0);
    for (var n = 0; n < casted_values.length; n++) {
      for (var m = 0; m < casted_values[n].length; m++) {
        column_header[m][n] = {
          property: 'header_key',
          data: casted_values[n][m]
        };
      }
    }
    for (var y = 0; y < keyLength; y++) {
      keyArr.push({
        property: 'header_key',
        column_header_header: true,
        data: chartConfig.keys[y].alias ? chartConfig.keys[y].alias : chartConfig.keys[y].col
      });
      emptyList.push({
        property: 'header_empty',
        data: null
      });
    }
    for (var j = 0; j < column_header.length; j++) {
      j == column_header.length - 1 ?
        column_header[j] = keyArr. concat(column_header[j]) :
        column_header[j] = emptyList.concat(column_header[j]);
    }
    var chartData = {
      chartConfig: chartConfig,
      data: column_header.concat(table_data)
    };
    table_data = null;
    column_header = null;
    return chartData;
    }

    private matrix(numrows, numcols, initial){
      var arr = [];
      for (var a = 0; a < numrows; ++a) {
        var columns = [];
        for (var s = 0; s < numcols; ++s) {
          columns[s] = initial;
        }
        arr[a] = columns;
      }
      return arr;
    }
}
